package com.atguigu.yygh.user.api;

import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.util.AuthContextHolder;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zr
 * @date 2021/12/16 15:30
 */
@Api(tags = "用户Api")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Resource
    private UserInfoService userInfoService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public Result loginUser(@RequestBody LoginVo loginVo) {
        Map<String, Object> userInfo = userInfoService.userLogin(loginVo);
        return Result.ok(userInfo);
    }

    @ApiOperation(value = "会员认证")
    @PostMapping("auth/userAuth")
    public Result userAuth(HttpServletRequest request, @RequestBody UserAuthVo userAuthVo) {
        // 传入用户Id
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok().message("认证成功");
    }

    /**
     * 获取用户id信息接口
     *
     * @return
     */
    @ApiOperation("获取用户信息")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        UserInfo userInfo = userInfoService.getById(AuthContextHolder.getUserId(request));
        return Result.ok(userInfo);
    }
}
